<?php

if (!defined("BASEPATH"))
    exit("No direct script access allowed");

class Base_controller extends CI_Controller {

    var $title_page;

    public function __construct() {
        parent::__construct();

        //load library
        $this->load->library('form_validation');
        $this->load->library("pagination");

        //load model
        $this->load->model("admin_model");
        $this->load->model("user_model");
        $this->load->model("order_model");
        $this->load->model("product_model");
        $this->load->model("feed_back_model");
        $this->load->model("pay_model");
        $this->load->model("cf_model");
        $this->load->model("menu_model");

        //load helper
        $this->load->helper("form");
        $this->load->helper("date");
        $this->load->helper("float");
        $this->title_page = FALSE;

        if (isset($_SERVER['HTTP_REFERER'])) {
            $this->session->set_userdata('previous_page', $_SERVER['HTTP_REFERER']);
        } else {
            $this->session->set_userdata('previous_page', base_url());
        }
    }

    public function render($content) {
        $data = array();

        $data["title_page"] = $this->title_page;
        $data["header_admin"] = $this->header_admin();
        $data["footer"] = $this->footer();
        $data["content"] = $content;
        $this->load->view('admin/common/main_view', $data);
    }

    public function header_admin() {
        $data = array();
        $sess = $this->session->userdata("logged_in_admin");
        if ($sess) {
            $data["username"] = $sess["user"];
        }
        return $this->load->view("admin/common/header_account", $data, TRUE);
    }

    public function footer() {
        $data = array();
        return $this->load->view("admin/common/footer_admin_view", $data, TRUE);
    }

    public function check_login() {
        if (!$this->isLogin()) {
            redirect("admin/main");
        }
    }

    public function isLogin() {
        return ($this->session->userdata('logined') && $this->session->userdata('ID'));
    }

    public function isAdmin() {
        if (strtolower($this->session->userdata("user_name") == "admin")) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function cfPag() {
        $config['full_tag_open'] = '<span class="pag">';
        $config['full_tag_close'] = '</span>';
        $config['num_tag_open'] = '<span class="pag_num">';
        $config['num_tag_close'] = '</span>';
        $config['first_link'] = 'Trước';
        $config['last_link'] = 'Sau';
        $config['cur_tag_open'] = '<span class="tag_cur">';
        $config['cur_tag_close'] = '</span>';
        $config['next_tag_open'] = '<span class="tag_next">';
        $config['next_tag_close'] = '</span>';
        $config['prev_tag_open'] = '<span class="tag_pre">';
        $config['prev_tag_close'] = '</span>';
        $config['uri_segment'] = 3;

        $this->pagination->initialize($config);
    }

    public function SetMenu($key) {
        $data = array(
            "ThongKe" => array("main" => "select", "sub" => ""),
            "SanPham" => array("main" => "select", "sub" => ""),
            "DanhMuc" => array("main" => "select", "sub" => ""),
            "HoaDon" => array("main" => "select", "sub" => ""),
            "KH" => array("main" => "select", "sub" => ""),
            "ConfigWeb" => array("main" => "select", "sub" => ""),
            "menu" => array("main" => "select", "sub" => "")
        );
        if (isset($key) && $key != NULL) {
            if (array_key_exists($key, $data)) {
                $data[$key]["main"] = "current";
                $data[$key]["sub"] = "show";
            }
        }
        return $data;
    }

    public function permissions_view() {
        $quyen_xem = array();
        if (!$this->isAdmin()) {
            $admin_group_id = $this->session->userdata("group");
            $query = $this->admin_model->read($admin_group_id);
            if (count($query) > 0) {
                $quyen_xem = unserialize($query->permission_view);
            }

            $url = $this->uri->segment(1) . '/' . $this->uri->segment(2) . '/' . $this->uri->segment(3);
            $url_per = $this->uri->segment(1) . '/' . $this->uri->segment(2);
            $igone = array("dienhoa-administrator/account/form_creat_acc","dienhoa-administrator/account/form_creat_group");
            if (!in_array($url, $igone)) {
                if (count($quyen_xem) > 0) {
                    if (!in_array($url_per, $quyen_xem)) {
                        redirect('dienhoa-administrator/permission/error');
                    } else {
                        return TRUE;
                    }
                }
            } else {
                redirect("dienhoa-administrator/permission/error");
            }
        } else {
            return TRUE;
        }
        return FALSE;
    }

    public function permission_del_edit() {
        $quyen_xoa = array();

        if (!$this->isAdmin()) {
            $admin_group_id = $this->session->userdata("group");
            $query = $this->admin_model->read($admin_group_id);
            if (count($query) > 0) {
                $quyen_xoa = unserialize($query->permission_edit_del);
            }

            $url = $this->uri->segment(1) . '/' . $this->uri->segment(2) . '/' . $this->uri->segment(3);
            $url_per = $this->uri->segment(1) . '/' . $this->uri->segment(2);

            if ($url_per) {
                if (count($quyen_xoa) > 0) {
                    if(!in_array($url_per, $quyen_xoa)){
                        redirect("dienhoa-administrator/permission/error");
                    }else{
                        return TRUE;
                    }
                }
            }
        }else{
            return TRUE;
        }
        return FALSE;
    }

}

?>
